package com.dats.util;


import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author sig
 * @since 2016/9/25
 */
public class Md5Util {

    public static String md5(String string) {
        return md5(string.getBytes());
    }

    public static String md5(byte[] input) {
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            byte[] bytes = digest.digest(input);
            return bytes2HexString(bytes);
        } catch (Exception e) {
            LogUtil.console.error(e.getMessage(), e);
            return null;
        }
    }

    private static String bytes2HexString(byte[] bytes) {
        StringBuilder builder = new StringBuilder();

        for (byte tmp : bytes)
            builder.append(Integer.toHexString(tmp >> 4 & 15))
                    .append(Integer.toHexString(tmp & 15));
        return builder.toString();
    }
}
